package com.promise.jokerdream.work.signin.dream

import com.promise.jokerdream.manager.ExecutionHistoryManager
import com.promise.jokerdream.repository.GameApi
import com.promise.jokerdream.utils.AppContext
import com.promise.jokerdream.work.BaseWork
import com.promise.jokerdream.work.WorkResult

// 梦幻之旅工作
class DreamTripWork(
    private val useDreamTicket: Boolean = false,
    uin: String
): BaseWork(uin) {

    override val name: String
        get() = "梦幻之旅"

    override val priority: Int
        get() = 2

    private val gameApi = GameApi.getInstance()
    private val historyManager by lazy { ExecutionHistoryManager.getInstance(AppContext.get()) }

    override suspend fun executeWork(): WorkResult {
        // 1. 先查询状态，bmapid=0跟随当前周期
        val queryResult = gameApi.queryDreamTrip(0)
        queryResult.fold(
            onSuccess = { response ->
                // 2. 检查是否可以使用普通机票
                if (response.normalTicket > 0) {
                    val normalTicketResult = gameApi.useNormalTicket()
                    normalTicketResult.fold(
                        onSuccess = { normalResponse ->
                            historyManager.addHistory(name, normalResponse.getDescription())
                        },
                        onFailure = { exception ->
                            historyManager.addHistory(name, exception.message)
                        }
                    )
                }

                // 3. 如果需要使用梦幻机票
                if (useDreamTicket) {
                    // 统计未完成的地点数量（status == 0）
                    val incompleteCount = response.smapInfo.count { it.status == 0 }
                    val dreamTicketCount = response.dreamTicket
                    
                    // 如果梦幻机票数量大于等于未完成的数量
                    if (dreamTicketCount >= incompleteCount) {
                        // 对未完成的地方使用梦幻机票
                        response.smapInfo.forEach { smapInfo ->
                            if (smapInfo.status == 0) {
                                val dreamTicketResult = gameApi.useDreamTicket(smapInfo.id)
                                dreamTicketResult.fold(
                                    onSuccess = { dreamResponse ->
                                        historyManager.addHistory(name, dreamResponse.getDescription())
                                    },
                                    onFailure = { exception ->
                                        historyManager.addHistory(name, exception.message)
                                    }
                                )
                            }
                        }
                    } else {
                        historyManager.addHistory(name, "梦幻机票数量不足，需要${incompleteCount}张，当前有${dreamTicketCount}张")
                    }
                }
            },
            onFailure = { exception ->
                historyManager.addHistory(name, exception.message)
            }
        )

        return WorkResult.Success
    }
}